/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tank.controllers;

import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import tank.models.GameSettings;
import tank.models.Global;
import static tank.models.Global.viewList;
import tank.views.TankSelectView;

/**
 *
 * @author shengchang
 */
public class TankSelectController implements MouseListener, MouseMotionListener{

    private TankSelectView view;
    private JFrame frame;
    
    public TankSelectView getView()
    {
        return this.view;
    }
    
    public void setView(TankSelectView view)
    {
        this.view = view;
    }
    
    public JFrame getFrame()
    {
        return this.frame;
    }
    
    public void setFrame(JFrame frame)
    {
        this.frame = frame;
    }
    
    @Override
    public void mouseClicked(MouseEvent e) {
        
        //change enemy tank type
        if( e.getX() < 200 && e.getX() > 0 && e.getY() < 399 && e.getY() > 199 )
        {
            if(Global.enemyTankType < Global.enemyTankTypeVector.size() - 1 )
                Global.enemyTankType++; 
            else
                Global.enemyTankType--; 
            view.repaint();
            new SoundPlayer(GameSettings.changeAudio).start();
        }
        
        //change my tank type
        if( e.getX() < 800 && e.getX() > 600 && e.getY() < 399 && e.getY() > 199 )
        {
            if(Global.myTankType < Global.myTankTypeVector.size() - 1 )
                Global.myTankType++; 
            else
                Global.myTankType--; 
            view.repaint();
            new SoundPlayer(GameSettings.changeAudio).start();
        }
        
        //change map background type
        if( e.getX() < 500 && e.getX() > 300 && e.getY() < 150 && e.getY() > 0 )
        {
            if(Global.mapBackgroundType < Global.mapBackgroundTypeVector.size() - 1 )
                Global.mapBackgroundType++; 
            else
                Global.mapBackgroundType--; 
            view.repaint();
            new SoundPlayer(GameSettings.changeAudio).start();
        }
        
        //when click "KO" then change to play view
        if( e.getX() < 550 && e.getX() > 250 && e.getY() < 450 && e.getY() > 200 )
        {
            //init map file according to the selection
            Global.map.setBackgroundImage(Global.mapBackgroundTypeVector.get(Global.mapBackgroundType));
            Global.map.setItemList(Global.mapLevelVector.get(Global.mapLevel));
            
            //change to play view
            frame.remove(view);
            frame.setTitle("Tank Game V1.0-Play");
            Global.viewList.get(1).setPreferredSize(new Dimension(800, 600));
            frame.add(Global.viewList.get(1));
            frame.pack();
            frame.setVisible(true);
            new SoundPlayer(GameSettings.changeAudio).start();
            Thread t = new Thread((Runnable) Global.viewList.get(1));
            t.start();
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseDragged(MouseEvent e) {
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }
    
}
